package besta.moon.sql.expression;

import besta.moon.MoonConnection;
import besta.moon.command.CommandSelect;
import besta.moon.engine.Index;
import besta.moon.sql.Expression;
import besta.moon.MoonException;
import besta.moon.MoonCode;

public class ExpressionInSelect extends ExpressionArithmetic {

    final private CommandSelect cmdSel;
    final private Index index = new Index(true);
    final private MoonConnection con;

    public ExpressionInSelect(MoonConnection con, Expression left,
            CommandSelect cmdSel, int operation) {
        super(left, (Expressions) null, operation);
        this.cmdSel = cmdSel;
        this.con = con;
    }

    private void loadInList() throws Exception {
        if (cmdSel.compile(con)) {
            cmdSel.from.execute();
            if (cmdSel.columnExpressions.size() != 1) {
                throw MoonException.create(MoonCode.SUBQUERY_COL_COUNT, cmdSel.columnExpressions.size());
            }
            index.clear();
            while (cmdSel.next()) {
                try {
                    index.addValues(0, cmdSel.columnExpressions);
                } catch (Exception e) {
                 
                }
            }
        }
    }

    @Override
    boolean isInList() throws Exception {
        loadInList();
        return index.findRows(getParams(), false, null) != null;
    }
}
